package com.imau.servlet;

import com.imau.bean.User;
import com.imau.mapper.UserMapper;
import com.imau.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private SqlSession sqlSession;
    private UserMapper userMapper;

    @Override
    public void init() throws ServletException {
        this.sqlSession = SqlSessionUtil.getSqlSession();
        this.userMapper = sqlSession.getMapper(UserMapper.class);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //接受前端的用户名和密码
        response.setContentType("text/html;charset=UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String remember = request.getParameter("remember");

        User user = userMapper.selectByUserNameAndPassword(username, password);

        if (user != null) {
            response.sendRedirect(request.getContextPath() + "/admin.html");
        } else {
            response.sendRedirect(request.getContextPath() + "/login.html");
        }


    }
}
